package br.mikhas.refractor.field;

import java.lang.reflect.Field;


/**
 * Implements the field proxy using the Unsafe object to access and modify field
 * values.
 * 
 * @author Mikhail Domanoski
 * 
 */
public class UnsafeObjectFieldProxy extends UnsafeClassFieldProxy implements
		FieldProxy {

	protected final Object object;

	public UnsafeObjectFieldProxy(Class<?> clazz, Field field, Object object) {
		super(clazz, field);
		this.object = object;
	}

	public void set(Object value) {
		if (this.isStatic()) {
			this.internalSet(this.clazz, value);
		} else {
			this.internalSet(this.object, value);
		}
	}

	@SuppressWarnings("unchecked")
	public <T> T get() {
		if (this.isStatic()) {
			return (T) this.internalGet(this.clazz);
		} else {
			return (T) this.internalGet(this.object);
		}
	}

	public String name() {
		return this.field.getName();
	}
}
